﻿using System;
using NHibernate;
using NUnit.Framework;

namespace NMA.Infrastructure.NHibernate.Tests.Mapping
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;       

    [TestFixture]
    public class UserTest : TestBaseClass
    {
        [Test]
        public void Can_add_user_to_database()
        {
            var address = new Address("123 Tan Hoa", "Hoa Thanh", "Tan Phu");

            var user = new User
            {
                Id = new Guid(),
                UserName = "test user name",
                Password = "test password",
                IsAlive = true,
                Description = "description",
                Address = address,
                BelongToRole = (int)Role.User,                
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now                
            };
            try
            {
                ISession session = SessionFactory.OpenSession();
                Guid id = (Guid)session.Save(user);
                session.Flush();

                session.Evict(user);

                var userDb = session.Get<User>(id);

                Assert.IsTrue(userDb.Id == user.Id && userDb.UserName == user.UserName);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ISession session = SessionFactory.OpenSession();
                session.Delete(user);
                session.Flush();
                session.Evict(user);
            }
        }

        [Test]
        public void Can_update_user_to_database()
        {
            var address = new Address("123 Tan Hoa", "Hoa Thanh", "Tan Phu");

            var user = new User
            {
                Id = new Guid(),
                UserName = "test user name",
                Password = "test password",
                IsAlive = true,
                Description = "description",
                Address = address,
                BelongToRole = (int)Role.User,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            };
            try
            {
                ISession session = SessionFactory.OpenSession();
                Guid id = (Guid)session.Save(user);
                session.Flush();

                session.Evict(user);

                var userDb = session.Get<User>(id);

                userDb.UserName = "test update";

                session.Update(userDb);
                session.Flush();

                session.Evict(userDb);

                var updateUserDb = session.Get<User>(userDb.Id);

                Assert.IsTrue(updateUserDb.Id == userDb.Id && updateUserDb.UserName == userDb.UserName);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ISession session = SessionFactory.OpenSession();
                session.Delete(user);
                session.Flush();
                session.Evict(user);
            }

        }
    }
}
